layui.use(['layer','form','table','element'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,element = layui.element;


    //动态加载下拉框
    $.ajax({
        type:"GET",
        url:"/admin/project/getLabelAndType",
        success:function (result) {
            var typeList = result.data.labelTypeList.typeList;

            $.each(typeList,function (i,type) {
                var typeName = type.typeName;
                var id = type.id;

                //类型过滤首页
                if(id != 0 ){
                    $("#type").append("<option value='"+id+"'>"+typeName+"</option>");
                }

            })

            //重新渲染下拉框
            form.render("select");
        },
        error:function () {
            layer.msg("系统错误！");
        }
    });

    //是否管理员
    var isAdmin = $("#isAdmin").val();
    var column;
    if(isAdmin == "true"){
        column = [[
            {field: 'id', title: 'ID', width: 80}
            ,{field:'title',width:300, title: '标题',toolbar: '#titleBar'}
            ,{field:'profit',title: '免费赚',width:100,edit:'profit',align:'center'}
            ,{field:'sortIndex',title: '排序',width:100,edit:'sortIndex',align:'center'}
            ,{field:'',title: '首页显示',width:100,toolbar: '#barDemo3',align:'center'}
            ,{field:'',title: '置顶',width:100,toolbar: '#effectBar',align:'center'}
            ,{field:'todayReadCount',title: '今日阅读',width:120,align:'center',sort: true, toolbar: '#readBar'}
            ,{field:'showCreateTime',width:110,title: '创建时间'}
            ,{field:'',width:520,title: '操作',toolbar: '#barDemo'}
        ]];
    }else{
        column = [[
            {field:'title', title: '标题',toolbar: '#titleBar'}
            ,{field:'readMount', width:90,title: '阅读数', align:'center'}
            ,{field:'types', width: 100   , title: '类型'}
            ,{field:'showCreateTime',width:110,title: '创建时间'}
            ,{field:'',width:520,title: '操作',toolbar: '#barDemo2'}
        ]];
    }

    //初始化数据表格
     table.render({
         elem:"#data"
         ,height: 'full-150'
         ,url:"/admin/project/getNormalProjectList"
         ,cellMinWidth: 80
         ,cols: column
         ,page:true
     });

    table.on('sort(dataTable)', function(obj) {
        var sortField = obj.field;
        var sortOrder = obj.type;
        var sortWhere = {
            "sortField":sortField,
            "sortOrder":sortOrder
        }
        table.reload("data",{
            initSort: obj
            ,where: sortWhere
        });
    });

    //监听指定开关
    form.on('switch(switch)', function(data){
        var id =  data.othis.parents('tr').find("td :first").text();
        var prams = {
            id:id,
            isHomePage:this.checked?"1":"0"
        }
        $.ajax({
            type:"POST"
            ,url:"/admin/project/setIsShowHome"
            ,data:prams
            ,success:function (result) {
                layer.msg(result.message);
            }
            ,error:function () {
                layer.msg("系统错误");
            }
        });
    });

    //监听指定开关
    form.on('switch(commented)', function(data){
        var id =  data.othis.parents('tr').find("td :first").text();
        var prams = {
            id:id,
            recommend:this.checked?"1":"0"
        }
        $.ajax({
            type:"POST"
            ,url:"/admin/project/setRecommend"
            ,data:prams
            ,success:function (result) {
                layer.msg(result.message);
            }
            ,error:function () {
                layer.msg("系统错误");
            }
        });
    });

    //监听单元格编辑
    table.on('edit(dataTable)', function(obj){
        var value = obj.value //得到修改后的值
            ,data = obj.data //得到所在行所有键值
            ,field = obj.field
        if("sortIndex" == field){
            var params = {
                id:data.id,
                sortIndex:value
            }
            $.ajax({
                type:"POST"
                ,url:"/admin/project/setSortIndex"
                ,data:params
                ,success:function (result) {
                    layer.msg(result.message);
                }
                ,error:function () {
                    layer.msg("系统错误");
                }
            });
        }else if("profit" == field){
            var params = {
                id:data.id,
                profit:value
            }
            $.ajax({
                type:"POST"
                ,url:"/admin/project/setProfit"
                ,data:params
                ,success:function (result) {
                    layer.msg(result.message);
                }
                ,error:function () {
                    layer.msg("系统错误");
                }
            });
        }

    });

     //监听工具条
     table.on('tool(dataTable)',function (obj) {
         var data = obj.data;
         if(obj.event == 'detail'){
             //查看项目详情
             var url = "/admin/project/descAndEdit/"+data.id;
             $(this).attr("data-url",url);
             parent.addTab($(this))

         }else if(obj.event == 'del'){
            //删除项目

             layer.confirm('确定删除？', function(index){
                 layer.msg('删除中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });

                 $.ajax({
                    type:"POST"
                     ,url:"/admin/project/delete"
                     ,data:{
                        id:data.id
                     }
                     ,success:function (result) {
                         layer.msg(result.message);
                         table.reload("data");
                     }
                     ,error:function () {
                         layer.msg("系统错误");
                     }

                 });

             });
         }else if(obj.event == 'copyUrl'){
             var projectId = data.id;
             var typeId = data.typeObj.id;
             var host = location.origin;
             var url = host+"/article/"+projectId ;
             $("#board").val(url);

             var input = document.querySelector("#board");
             input.select();
             if (document.execCommand('copy')) {
                 document.execCommand('copy');
                layer.msg("复制成功！");
             }

         }else if(obj.event == 'commit_baidu'){
             //提交到百度
             var projectId = data.id;
             var host = location.origin;
             var url = host+"/article/"+projectId ;

             $.ajax({
                 type:"POST",
                 data:{
                     "type":1,
                     "url":url
                 }
                 ,url:"/admin/system/commit"
                 ,success:function (result) {
                     layer.confirm(result.message, function(index){
                         layer.close(index);
                     });
                 }
                 ,error:function () {
                     layer.msg("系统错误");
                 }

             });
         }else if(obj.event == 'baidu_fast'){
             //提交到百度快速收录
             var projectId = data.id;
             var host = location.origin;
             var url = host+"/article/"+projectId ;

             $.ajax({
                 type:"POST",
                 data:{
                     "type":2,
                     "url":url
                 }
                 ,url:"/admin/system/commit"
                 ,success:function (result) {
                     layer.confirm(result.message, function(index){
                         layer.close(index);
                     });
                 }
                 ,error:function () {
                     layer.msg("系统错误");
                 }

             });
         }else if(obj.event == 'bing'){
             //提交到bing收录
             var projectId = data.id;
             var host = location.origin;
             var url = host+"/article/"+projectId ;

             $.ajax({
                 type:"POST",
                 data:{
                     "type":3,
                     "url":url
                 }
                 ,url:"/admin/system/commit"
                 ,success:function (result) {
                     layer.confirm(result.message, function(index){
                         layer.close(index);
                     });
                 }
                 ,error:function () {
                     layer.msg("系统错误");
                 }

             });
         }else if(obj.event == 'fankui'){
             var url = "/admin/project/freeToAdd?source=withdraw&projectId="+data.id;
             $(this).attr("data-url",url);
             parent.addTab($(this));
         }else if(obj.event == 'process'){
             var url = "/admin/project/freeToAdd?source=process&projectId="+data.id;
             $(this).attr("data-url",url);
             parent.addTab($(this));
         }else if(obj.event == 'copyTitleUrl'){
             var projectId = data.id;
             var title = data.title;
             var host = location.origin;
             var url = host+"/article/"+projectId ;
             var content = title + "：" + url
             $("#board").val(content);

             var input = document.querySelector("#board");
             input.select();
             if (document.execCommand('copy')) {
                 document.execCommand('copy');
                 layer.msg("复制成功！");
             }
         }else if(obj.event == 'unline'){
             var projectId = data.id;

             layer.confirm('确定下架？', function(index){
                 layer.msg('下架中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });

                 $.ajax({
                     type:"POST",
                     data:{
                         "projectId":projectId
                     }
                     ,url:"/admin/project/unline"
                     ,success:function (result) {
                         layer.confirm(result.message, function(index){
                             layer.close(index);
                         });
                         table.reload("data");
                         form.render();
                     }
                     ,error:function () {
                         layer.msg("系统错误");
                     }

                 });

             });
         }else {
             //编辑项目
            layer.msg("操作错误！");
         }
     });

    function rightMenu(){

        $(".layui-tab-title li",window.parent.document).on("contextmenu",function (e) {
            var rightmenu = $(".rightmenu",window.parent.document);
            rightmenu.find("li").attr('data-id',$(this).attr("lay-id"));
            l = e.clientX;
            t = e.clientY;
            rightmenu.css({ left:l, top:t}).show();
            return false;
        });
        //左键点击隐藏
        $("body,.layui-tab-title li",window.parent.document).click(function(){
            $(".rightmenu").hide();
        });

    }

    function iframeWH(){
        var H = $(window).height();
        $("iframe",window.parent.document).css("height",H+"px");
    }
     
     //搜索
    $("#search").on("click",function () {
        table.reload("data",{
            page:{
                curr:1
            }
            ,where:{
                title:$("#title").val(),
                typeId:$("#type").val(),
                labelId:$("#label").val(),
                commented:$("#commented").val(),
                createId:$("#authorId").val(),
                commented:$("#commend").val(),
            }
        })
    });;

    $("#add").on("click",function () {

        parent.addTab($(this));

        // parent.layui.element.tabAdd('tabs',{
        //     title:"免费项目新增",
        //     content:"<iframe  src='/admin/project/freeToAdd'  frameborder='0'  style='width:100%;height: 100%'></iframe>",
        //     id:"free_project_add"
        // });
        // rightMenu();
        // iframeWH();
        // parent.layui.element.tabChange('tabs','free_project_add');


    });

    $("#updateSiteMap").on("click",function () {
        $.ajax({
            type:"POST"
            ,url:"/admin/system/updateSiteMap"
            ,success:function (result) {
                layer.msg(result.message);
            }
            ,error:function () {
                layer.msg("系统错误");
            }

        });
    });
});

function toAdd() {
    window.open("/admin/project/freeToAdd", "_blank");
}